#pragma once

#include <iostream>
#include "Agreement.hpp"
#include "NetCalor.hpp"
#include <functional>

using func_t = std::function<Result(Demd _demand)>;

class PraseMessage
{
public:
    PraseMessage(func_t func)
        : _func(func)
    {
    }

    ~PraseMessage()
    {
    }

    std::string Prase(std::string &buffer)
    {
        std::string _sendmessage;
        while (1)
        {

            std::string _ret = Agreement::UnPackage(buffer);
            if(_ret.empty())
                break;
            Demd demand;
            demand.Deserialize(_ret);
            Result _res = _func(demand);

            _res.Serialize(&_ret);
            _sendmessage += _ret;
        }
        std::string _ret = Agreement::Package(_sendmessage);
        return _ret;
    }

private:
    func_t _func;
};